﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Interface_Admin.ServiceReference;

namespace Interface_Admin
{
    class Program
    {
        static void Main(string[] args)
        {

            System.Console.Write("#####################################\n"
                 + "#      Gallery Admin Interface      #\n"
                 + "# By Mathieu BIVERT and Mathieu RUL #\n"
                 + "#####################################\n"
                 + "\nPlease login!\n");

            try
            {
                ServiceClient serviceClient = new ServiceClient();
                string uid = null;
                string login = null;
                while (null == uid)
                {
                    System.Console.Write("Login: ");
                    login = System.Console.ReadLine();
                    System.Console.Write("Password: ");
                    string password = System.Console.ReadLine();
                    uid = serviceClient.connectUser(login, password);
                    if (null == uid)
                    {
                        System.Console.Write("Error: wrong login/password!\n");
                    }
                }

                System.Console.Write("Login succeed, type help to see the available commands!\n");

                string cmd;
                bool quit = false;
                while (!quit)
                {
                    Console.Write(login + " $ ");
                    cmd = Console.ReadLine();
                    string[] a = cmd.Split(new Char[] { ' ', '\t' });
                    switch (a[0])
                    {
                        case "adduser":
                            if (a.Length >= 4)
                                Console.WriteLine(serviceClient.addUser(a[1], a[2], a[3]));
                            else
                                Console.WriteLine("adduser name login password");
                            break;
                        case "deleteuser":
                            if (a.Length >= 2)
                                Console.WriteLine(serviceClient.deleteUser(a[1]) ? "User #" + a[1] + " deleted successfully !" : "Error: user #" + a[1] + " not deleted !");
                            else
                                Console.WriteLine("deleteuser id");
                            break;
                        case "listuser":
                            String[] users = serviceClient.getUserList();
                            for (int i = 0; i < users.Length; i++)
                            {
                                Console.WriteLine(users[i]);
                            }
                            break;
                        case "listuseralbum":
                            String[] userAlbums = serviceClient.getUserAlbum((a.Length >= 2) ? a[1] : uid);
                            for (int i = 0; i < userAlbums.Length; i++)
                            {
                                Console.WriteLine(userAlbums[i]);
                            }
                            break;
                        case "addalbum":
                            if (a.Length >= 1)
                                Console.WriteLine(serviceClient.addAlbum(a[1], (a.Length >= 3) ? a[2] : uid));
                            else
                                Console.WriteLine("addalbum name [userid]");
                            break;
                        case "deletealbum":
                            if (a.Length >= 2)
                                Console.WriteLine(serviceClient.deleteAlbum(a[1]) ? "Album #" + a[1] + " deleted successfully !" : "Error: album #" + a[1] + " not deleted !");
                            else
                                Console.WriteLine("deletealbum id");
                            break;
                        case "listalbum":
                            String[] albums = serviceClient.getAlbumList();
                            for (int i = 0; i < albums.Length; i++)
                            {
                                Console.WriteLine(albums[i]);
                            }
                            break;
                        case "listalbumpicture":
                            if (a.Length >= 2)
                            {
                                String[] albumPictures = serviceClient.getAlbumPicture(a[1]);
                                for (int i = 0; i < albumPictures.Length; i++)
                                {
                                    Console.WriteLine(albumPictures[i]);
                                }
                            }
                            else
                                Console.WriteLine("listalbumpicture albumId");
                            break;
                        case "addpicture":
                            if (a.Length >= 3)
                            {
                                byte[] picture = readFile(a[3]);
                                if (null != picture)
                                {
                                    Console.WriteLine("Picture added #" + serviceClient.addPicture(a[1], a[2], picture));
                                }
                            }
                            else
                                Console.WriteLine("addpicture albumId name src");
                            break;
                        case "deletepicture":
                            if (a.Length >= 2)
                                Console.WriteLine(serviceClient.deletePicture(a[1]) ? "Picture #" + a[1] + " deleted successfully !" : "Error: picture #" + a[1] + " not deleted !");
                            else
                                Console.WriteLine("deletepicture id");
                            break;
                        case "listpicture":
                            String[] pictures = serviceClient.getPictureList();
                            for (int i = 0; i < pictures.Length; i++)
                            {
                                Console.WriteLine(pictures[i]);
                            }
                            break;
                        case "getpicture":
                            if (a.Length >= 3)
                            {
                                if (writeFile(serviceClient.getPicture(a[1]), a[2]))
                                {
                                    Console.WriteLine("Picture #" + a[1] + " writed at: " + a[2]);
                                }
                                else
                                {
                                    Console.WriteLine("Error: Picture #" + a[1] + " not writed at: " + a[2]);
                                }
                            }
                            else
                                Console.WriteLine("getpicture id dest");
                            break;
                        case "help":
                            help();
                            break;
                        case "quit":
                            quit = true;
                            break;
                        default:
                            Console.WriteLine("Error: unknown command: " + a[0]);
                            break;
                    }
                }
                serviceClient.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error:" + e.Message);
                Console.ReadLine();
            }
        }

        private static void help()
        {
            Console.WriteLine("Command list:\n"
                + "adduser name login password\n"
                + "deleteuser id\n"
                + "listuser\n"
                + "listuseralbum [id]\n"
                + "\n"
                + "addalbum name [userId]\n"
                + "deletealbum id\n"
                + "listalbum\n"
                + "listalbumpicture albumId\n"
                + "\n"
                + "addpicture albumId name src\n"
                + "deletepicture id\n"
                + "listpicture\n"
                + "getpicture id dest\n"
                + "\n"
                + "help\n"
                + "quit\n");
        }

        private static byte[] readFile(string path)
        {
            byte[] data = null;
            try
            {
                FileInfo fileInfo = new FileInfo(path);
                int nbBytes = (int)fileInfo.Length;
                FileStream fileStream = new FileStream(path, FileMode.Open,
                FileAccess.Read);
                BinaryReader br = new BinaryReader(fileStream);
                data = br.ReadBytes(nbBytes);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: Reading file:" + path + "\nException : " + e.Message);
            }
            return data;
        }

        private static bool writeFile(byte[] data, string path)
        {
            try
            {
                FileStream fileStream = new FileStream(path, FileMode.Append, FileAccess.Write);
                BinaryWriter bw = new BinaryWriter(fileStream);
                bw.Write(data);
                bw.Close();
                fileStream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: Writing file:" + path + "\nException : " + e.Message);
                return false;
            }
            return true;
        }
    }
}
